package com.ruoyi.data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DataUtils {
    /**
     *  取整
     * @param data
     * @return
     */
    public static Double doubleCeil(Double data) {
        Integer i =  (int)Math.ceil(data);
        return Double.valueOf(i.toString());
    }
    /**
     *  取整四舍五入
     * @param data
     * @return
     */
    public static Double doubleFloor(Double data) {
        long i =  (int)Math.round(data);
        return Double.valueOf(i);
    }
    /**
     *  取整舍余
     * @param data
     * @return
     */
    public static Double intFloor(Double data) {
        long i =  (int)Math.floor(data);
        return Double.valueOf(i);
    }

    /**
     *  保留两位小数舍余
     * @param data
     * @return
     */
    public static Double saveTwoTruncation(Double data) {
        double i=((double)((int)(data*100)))/100;
        return Double.valueOf(i);
    }
    /**
     *  保留一位小数舍余
     * @param data
     * @return
     */
    public static Double saveOneTruncation(Double data) {
        double i=((double)((int)(data*10)))/10;
        return Double.valueOf(i);
    }

//	public static Double doubleScale(Double data){
//		BigDecimal dataDecimal = new BigDecimal(data);
//		double returnData = dataDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
//		return returnData;
//	}


    public static BigDecimal sumMoney(BigDecimal moneyA, Double moneyB){
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        return moneyA.add(decimalB);
    }

    /*乘法保留4位小数四舍五入*/
    public static Double multiplyMoney(Double moneyA, Double moneyB){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        return doubleScale(Double.valueOf(decimalA.multiply(decimalB).toString()));
    }

    /*乘法保留2位小数四舍五入*/
    public static Double multiplyTwo(Double moneyA, Double moneyB){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        return doubleScaleTwo(Double.valueOf(decimalA.multiply(decimalB).toString()));
    }
    /*乘法保留1位小数四舍五入*/
    public static Double multiplyOne(Double moneyA, Double moneyB){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        return doubleScale1(Double.valueOf(decimalA.multiply(decimalB).toString()));
    }

//    public static void main(String[] args) {
//        Double a = 124.31;
//        Double b = 67.5;
//        Double c=DataUtils.multiplyTwo(a,b);
//        System.out.println(c);
//    }

    public static Double doubleScaleTwo(Double data){
        BigDecimal decimalA = new BigDecimal(data.toString());
        BigDecimal decimalB = new BigDecimal(100);
        return Double.valueOf(doubleFloor((Double.valueOf(decimalA.multiply(decimalB).toString()))))/100;
    }
    public static Double doubleScale1(Double data){
        return Double.valueOf(doubleFloor(data*10)/10);
    }

    public static Double doubleScale(Double data){
        DecimalFormat df = new DecimalFormat("#.0000");
        df.setRoundingMode(RoundingMode.HALF_UP);
        String dateStr = df.format(data);
        return Double.valueOf(dateStr);
    }

    public static Double doubleScale2(Double data){
        BigDecimal dataDecimal = new BigDecimal(data);
        double returnData = dataDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return returnData;
    }

    public static Double doubleScale4(BigDecimal data){
        BigDecimal dataDecimal = new BigDecimal(String.valueOf(data));
        double returnData = dataDecimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        return returnData;
    }

//	public static Double doubleScale2(Double data){
//		DecimalFormat df = new DecimalFormat("#.00");
//		df.setRoundingMode(RoundingMode.HALF_UP);
//		String dateStr = df.format(data);
//		return Double.valueOf(dateStr);
//	 }

    public static String sumMoney(Double moneyA, String moneyB){
        BigDecimal decimalA = new BigDecimal(moneyA);
        BigDecimal decimalB = new BigDecimal(moneyB);
        return decimalA.add(decimalB).toString();
    }

    public static Double multiplyMoney(Double moneyA, Double moneyB, Double moneyC){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        BigDecimal decimalC = new BigDecimal(moneyC.toString());
        return Double.valueOf(decimalA.multiply(decimalB).multiply(decimalC).toString());
    }

    public static Double sumMoney(Double moneyA, Double moneyB){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        return doubleScale(Double.valueOf(decimalA.add(decimalB).toString()));
    }

    public static Double sumMoney(Double moneyA, Double moneyB, Double moneyC){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        BigDecimal decimalC = new BigDecimal(moneyC.toString());
        return doubleScale(Double.valueOf(decimalA.add(decimalB).add(decimalC).toString()));
    }

    public static String subMoney(String moneyA, String moneyB){
        BigDecimal decimalA = new BigDecimal(moneyA);
        BigDecimal decimalB = new BigDecimal(moneyB);
        return decimalA.subtract(decimalB).toString();
    }
    //两数相减
    public static Double subMoney(Double moneyA, Double moneyB){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        return doubleScale(Double.valueOf(decimalA.subtract(decimalB).toString()));
    }

    public static Double subMoney(Double moneyA, Double moneyB, Double moneyC){
        BigDecimal decimalA = new BigDecimal(moneyA.toString());
        BigDecimal decimalB = new BigDecimal(moneyB.toString());
        BigDecimal decimalC = new BigDecimal(moneyC.toString());
        return doubleScale(Double.valueOf(decimalA.subtract(decimalB).subtract(decimalC).toString()));
    }

    //	public static void main(String[] args) {
//		Double a = 43.0;
//		String buf[] = a.toString().split("\\.");
//		if(Double.valueOf(buf[1]).compareTo(a)==0){
//			a = Double.valueOf(buf[1]);
//		}
//		System.out.println();
//	}
    /*除法保留四位小数*/
    public static Double divideMoney(Double moneyA, Double moneyB){
        return new BigDecimal((Double)moneyA/moneyB).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /*乘法保留4位小数*/
    public static Double spaceMoney(Double moneyA, Double moneyB){
        return new BigDecimal((Double)moneyA*moneyB).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static Double costMargin(Double moneyA, Double moneyB){
        return new BigDecimal((Double)moneyA-moneyB).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static Integer method(Integer moneyA, Integer moneyB){
        return new Integer(moneyA-moneyB);
    }

}
